<!-- 推荐商品 -->
<template>
  <div class="recommendation">
    <h2 class="section-title">为您推荐</h2>
    <div class="product-grid">
      <div 
        v-for="product in products" 
        :key="product.id" 
        class="product-item"
         @click="router.push({ name: 'product', params: { id: product.id } })"
      >
        <img :src="product.productMainImg" :alt="product.productName" class="product-image" />
        <div class="product-info">
          <h3 class="product-name">{{ product.productName }}</h3>
          <div class="price-section">
            <span class="price">¥{{ product.productPrice }}</span>
            <span class="original-price" v-if="product.productPrice">¥{{ (product.productPrice*1.1).toFixed(1) }}</span>
          </div>
          <button class="add-cart-btn" @click="addToCart(product)">加入购物车</button>
        </div>
      </div>
    </div>
    <div v-if="loading" class="loading-indicator">加载中...</div>
  </div>
</template>

<script setup>
  import { ref, onMounted, onUnmounted } from 'vue'
  import { useRouter } from 'vue-router'
  import { productAPI } from '@/http'

  const router = useRouter()

  const products = ref([]) // 用来存储商品数据
  const loading = ref(false) // 控制加载状态
  const page = ref(1) // 当前页面，初始为第1页

  // 加载更多商品
  const fetcheProduct = async (pageNum) => {
    loading.value = true
    const response = await productAPI.getRandomProduct(8, pageNum);  // 假设后端支持分页，传递页数
    if (response.data && response.data.length > 0) {
      products.value = [...products.value, ...response.data] // 追加新商品到现有的商品列表
    }
    loading.value = false
  }

  // 页面加载时，获取初始商品数据
  onMounted(() => {
    fetcheProduct(page.value)
    
    // 监听滚动事件，判断是否滑到底部
    window.addEventListener('scroll', onScroll)
  })

  // 滚动事件处理函数
  const onScroll = () => {
    const scrollY = window.scrollY
    const documentHeight = document.documentElement.scrollHeight
    const windowHeight = window.innerHeight
    
    // 判断是否滚动到底部
    if (scrollY + windowHeight >= documentHeight - 50 && !loading.value) {
      page.value += 1  // 加载下一页
      fetcheProduct(page.value)  // 请求新的商品数据
    }
  }

  // 组件销毁时移除滚动事件监听
  onUnmounted(() => {
    window.removeEventListener('scroll', onScroll)
  })

  const addToCart = (product) => {
    cartStore.addToCart(product)
  }
</script>
  
  <style scoped>
  .recommendation {
    margin-bottom: 40px;
  }
  .loading-indicator {
  text-align: center;
  padding: 20px;
  font-size: 16px;
  color: #ff6700;
}
  .product-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
  }
  
  .product-item {
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
    transition: all 0.3s;
  }
  
  .product-item:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  }
  
  .product-image {
    width: 100%;
    height: 200px;
    object-fit: cover;
  }
  
  .product-info {
    padding: 15px;
  }
  
  .product-name {
    font-size: 16px;
    margin-bottom: 10px;
    height: 40px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  
  .price-section {
    margin-bottom: 15px;
  }
  
  .price {
    color: #ff6700;
    font-size: 18px;
    font-weight: bold;
    margin-right: 10px;
  }
  
  .original-price {
    font-size: 14px;
    color: #999;
    text-decoration: line-through;
  }
  
  .add-cart-btn {
    width: 100%;
    padding: 8px 0;
    background: #ff6700;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s;
  }
  
  .add-cart-btn:hover {
    background: #ff4500;
  }
  </style>